package com.xxxy.controller;

import com.xxxy.commonResult.CommonResult;
import com.xxxy.commonResult.CommonResultPage;
import com.xxxy.log.Log;
import com.xxxy.model.KnowledgePoint;
import com.xxxy.model.Question;
import com.xxxy.response.ExaminationPaperResponse;
import com.xxxy.response.QuestionDetailResponse;
import com.xxxy.response.QuestionResponse;
import com.xxxy.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sjh
 * @date 2022/2/26 14:55
 */
@RestController
@Api(tags = "试题管理")
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    QuestionService questionService;

    @Log(module = "试题管理", description = "获取试题列表")
    @PostMapping(value = "/list")
    @ApiOperation(value = "试题列表")
    public CommonResult<CommonResultPage> listQuestion(@ApiParam("页数") @RequestParam(defaultValue = "1") Integer page,
                                                       @ApiParam("一页条数") @RequestParam(defaultValue = "10") Integer size,
                                                       @ApiParam("题目类型") @RequestParam Integer type,
                                                       @ApiParam("知识点") @RequestBody KnowledgePoint knowledgePoint) {
        return CommonResult.success(questionService.listQuestion(page, size, type, knowledgePoint));
    }

    @Log(module = "试题管理", description = "单个题目信息")
    @GetMapping(value = "/info/{id}")
    @ApiOperation(value = "单个题目信息")
    public CommonResult<QuestionDetailResponse> getQuestionById(@ApiParam("题目id") @PathVariable Integer id) {
        return CommonResult.success(questionService.getQuestionById(id));
    }

    @Log(module = "试题管理", description = "新增单个试题")
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增单个试题")
    public CommonResult<Boolean> addQuestion(@ApiParam("选择题信息") @RequestBody Question question) {
        if (questionService.save(question)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Log(module = "试题管理", description = "删除单个试题")
    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除单个试题")
    public CommonResult<Boolean> deleteQuestion(@ApiParam("试题id") @RequestParam Integer questionId) {
        if (questionService.removeById(questionId)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Log(module = "试题管理", description = "更新单个试题信息")
    @PostMapping(value = "/update")
    @ApiOperation(value = "更新单个试题信息")
    public CommonResult<Boolean> updateQuestion(@ApiParam("试题") @RequestBody Question question) {
        if (questionService.updateById(question)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
